<!DOCTYPE HTML>
<script src="../../../resources/js-test.js"></script>
<style>
div.box {
  margin: 10px;
  padding: 50px;
  float: left;
}
</style>

<div id="target" class="box" style="background-color:green;">
</div>

<div id="console"></div>

<script>
description("Verifies the effect of pointer event prevent-default on pointerdown & mouse/pen events.");

var mouseEventList = ["mousedown", "mouseup", "mouseenter", "mouseleave", "mouseover", "mouseout", "mousemove"];
var pointerEventList = ["pointerdown", "pointerup", "pointerenter", "pointerleave", "pointerover", "pointerout", "pointermove"];

var eventPointerTypeToPreventDefault = "";
var eventIsPrimaryToPreventDefault = false;
var seqNo = 0;

var rect = document.getElementById("target").getBoundingClientRect();
var x1 = rect.left - 3;
var y1 = rect.top - 5;
var x2 = rect.left + 3;
var y2 = rect.top + 5;

function init() {
  var targetDiv = document.getElementById("target");

  mouseEventList.forEach(function(eventName) {
    targetDiv.addEventListener(eventName, function(event) {
      debug(++seqNo + ". target received " + event.type);
    });
  });

  pointerEventList.forEach(function(eventName) {
    targetDiv.addEventListener(eventName, function(event) {
      var preventDefaultMsg = "";
      if (event.type == "pointerdown"
          && event.pointerType == eventPointerTypeToPreventDefault
          && event.isPrimary == eventIsPrimaryToPreventDefault) {
        event.preventDefault();
        preventDefaultMsg = ", prevented default";
      }

      debug(++seqNo + ". target received " + event.type + ' ' +  (event.isPrimary? 'primary ':'non-primary ') + event.pointerType + preventDefaultMsg);
    });
  });
}

function testScenario(pointerType, isPrimary) {
  seqNo = 0;
  eventPointerTypeToPreventDefault = pointerType;
  eventIsPrimaryToPreventDefault = isPrimary;
  debug("--- test with preventDefault on " + (isPrimary? "primary ":"non-primary ") + pointerType + " ---");

  debug("- start with mouse, primary pen and non-primary pen outside target -");
  eventSender.mouseMoveTo(x1, y1);
  eventSender.mouseMoveTo(x1, y1, [], "pen", 0);
  eventSender.mouseMoveTo(x1, y1, [], "pen", 1);
  eventSender.mouseDown(0);
  eventSender.mouseUp(0);


  debug("- move mouse into target & press -");
  eventSender.mouseMoveTo(x2, y2);
  eventSender.mouseDown(2);

  debug("- move primary pen into target & press -");
  eventSender.mouseMoveTo(x2+1, y2+1, [], "pen", 0);
  eventSender.mouseDown(2, [], "pen", 0);

  debug("- move non-primary pen into target & press -");
  eventSender.mouseMoveTo(x2+2, y2+2, [], "pen", 1);
  eventSender.mouseDown(2, [], "pen", 1);

  debug("- jiggle mouse in target -");
  eventSender.mouseMoveTo(x2+3, y2+3);

  debug("- jiggle non-primary pen in target -");
  eventSender.mouseMoveTo(x2+5, y2+5, [], "pen", 1);

  debug("- jiggle primary pen in target -");
  eventSender.mouseMoveTo(x2+4, y2+4, [], "pen", 0);

  debug("- release mouse -");
  eventSender.mouseUp(2);

  debug("- release non-primary pen -");
  eventSender.mouseUp(2, [], "pen", 1);

  debug("- release primary pen -");
  eventSender.mouseUp(2, [], "pen", 0);

  debug("");
}

function runTests() {

  testScenario("none", false);
  testScenario("mouse", true);
  testScenario("pen", true);
  testScenario("pen", false);

}

init();
if (window.eventSender)
  runTests();
else
  debug("This test requires eventSender");

</script>
